package com.goxueche.lib_core.ui.fragment;

import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;

import com.goxueche.lib_core.R;
import com.goxueche.lib_core.ui.mvp.presenter.IBasePresenter;
import com.goxueche.lib_core.widgets.viewhelper.VaryViewHelper;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.android.FragmentEvent;

public abstract class BaseFragment<P extends IBasePresenter> extends FrameBaseFragment {

    public VaryViewHelper mVaryViewHelper;

    private P mPresenter;


    @Override
    protected void initView(View view) {
        initImmersionBar();
        View contentView = findViewById(R.id.core_content_layout);
        if (contentView != null) {
            initVaryView();
        }
        findViewById();
        initLayoutView();
    }

    protected void initVaryView() {
        if (loadingview() <= 0 || emptyview() <= 0 || errorview() <= 0) {
            return;
        }
        mVaryViewHelper = new VaryViewHelper.Builder()
                .setDataView(findViewById(R.id.core_content_layout))//放数据的父布局，逻辑处理在该Activity中处理
                .setLoadingView(LayoutInflater.from(getActivity()).inflate(loadingview(), null))//加载页，无实际逻辑处理
                .setEmptyView(LayoutInflater.from(getActivity()).inflate(emptyview(), null))//空页面，无实际逻辑处理
                .setErrorView(LayoutInflater.from(getActivity()).inflate(errorview(), null))//错误页面
                .setRefreshListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reloadData();
                    }
                })//错误页点击刷新实现
                .build();
    }

    public abstract void findViewById();

    /**
     * 沉浸式状态栏
     */
    public abstract void initImmersionBar();

    /**
     * 初始化布局
     */
    public abstract void initLayoutView();

    public abstract int loadingview();

    public abstract int emptyview();

    public abstract int errorview();

    public void reloadData() {
        if (mVaryViewHelper != null) {
            mVaryViewHelper.showLoadingView();
        }
    }

    /**
     * 创建 Presenter
     *
     * @return Presenter
     */
    public abstract P onBindPresenter();

    /**
     * 获取 Presenter 对象，在需要获取时才创建`Presenter`，起到懒加载作用
     */
    public P getPresenter() {
        if (mPresenter == null) {
            mPresenter = onBindPresenter();
        }
        return mPresenter;
    }

    @Override
    public <T> LifecycleTransformer<T> getLifecycleTransformer() {
        return bindUntilEvent(FragmentEvent.DESTROY_VIEW);
    }

    @Override
    public Dialog showWaitDialog(boolean cancelable) {
        assert getActivity() != null;
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (null == dialog || !dialog.isShowing()) {
                    dialog = new Dialog(getContext(), R.style.dialog_transparent);
                    View waitingView = getLayoutInflater().inflate(
                            R.layout.circular_progress, null);
                    waitingView.invalidate();
                    dialog.setContentView(waitingView);
                }
                try {
                    dialog.setCancelable(cancelable);
                    dialog.show();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });
        return dialog;
    }

    public boolean isEmpty(Object str) {
        if (str == null || str.toString().trim().length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onDestroy() {
        if (mVaryViewHelper != null) {
            mVaryViewHelper = null;
        }
        super.onDestroy();
    }
}
